/**
 * Copyright (C), 2015-2018
 * FileName: FilterConfiguration
 * Author:   Dayu Hou
 * Date:     2018/1/10 11:38
 * Description:
 * History:
 * <author>          <time>         <desc>
 */
package com.apache.tomcat;

//import com.apache.ius.listener.IusParamsListener;
import com.apache.passport.common.JwtClientFilter;
import com.apache.plateform.interceptor.TokenInterceptor;
import com.apache.rpc.StartSocketPortListener;
import com.apache.tools.StrUtil;
import com.apache.uct.common.filter.UctServerFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.EventListener;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Dayu Hou
 * @since 1.0.0
 */
@Configuration
public class FilterConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截

        registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/**");
        // registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");

        super.addInterceptors(registry);
    }

    @Bean
    public StartSocketPortListener startSocketPortListener() {
        String socketPort = StrUtil.doNull(env.getProperty("rpc.service.port"), "20808");
        StartSocketPortListener listener =  new StartSocketPortListener(socketPort);
        listener.registry();
        return listener;
    }

    /**
     * 加载listener
     */
//    @Bean
//    public ServletListenerRegistrationBean<EventListener> getDemoListener() {
//        ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<>();
//        registrationBean.setListener(new IusParamsListener());
//        registrationBean.setOrder(2);
//        return registrationBean;
//    }

    @Bean
    public FilterRegistrationBean encodingFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CharacterEncodingFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("encoding", "UTF-8");
        registration.addInitParameter("forceEncoding", "true");
        registration.setName("encodingFilter");
        registration.setOrder(1);
        return registration;
    }

   /* @Bean
    public FilterRegistrationBean defaultBaseSecurityFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new JwtClientFilter());
        registration.addUrlPatterns("/*", "/logout", "/cset");
        registration.setName("ssoClientFilter");
        registration.addInitParameter("sysCode", "uct");
        registration.setOrder(2);
        return registration;
    }*/

    @Bean
    public FilterRegistrationBean uctServerFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new UctServerFilter());
        registration.addUrlPatterns("/*");
        registration.setName("uctServerFilter");
        registration.setOrder(3);
        return registration;
    }
}